<template>
  <div class="modal fade" :id="id" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <h4 class="modal-title">
            <slot name="title"> 退票修改 </slot>
          </h4>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <div class="card py-0">
            <div class="card-body py-0">
              <div class="row mb-0">
                <label class="col-form-label col-4 text-end"> 票号 </label>
                <div class="col-md-6">
                  <input
                    type="text"
                    readonly
                    class="form-control-plaintext"
                    v-model.trim="ticketNo"
                  />
                </div>
              </div>
              <div class="row mb-0">
                <label class="col-form-label col-4 text-end"> 乘客姓名 </label>
                <div class="col-md-6">
                  <input
                    type="text"
                    readonly
                    class="form-control-plaintext"
                    v-model="psgName"
                  />
                </div>
              </div>
              <div class="row mb-1">
                <label class="col-form-label col-4 text-end"> 金额 </label>
                <div class="col-md-6">
                  <input
                    type="text"
                    class="form-control"
                    v-model.number="amount"
                  />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";

export default {
  props: {
    id: {
      type: String,
      default: "modalPrompt",
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      ticketNo: "",
      psgName: "",
      amount: 0,

      myModal: null
    };
  },
  methods: {
    ok: function () {
      this.hideModal();

      this.resolve({
        ticketNo: this.ticketNo,
        name: this.psgName,
        amount: this.amount,
      });
    },
    modal: function (ticketNo, psgName, amount) {
      this.ticketNo = ticketNo;
      this.psgName = psgName;
      this.amount = amount;
      
      this.showModal();
      
      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });
      }

      return this.myModal;
    },
  },
};
</script>
